using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class MenuDal : Base
    {
        public MenuDal() : base() { }

        public enum Fields
        {
            Id,
            SiteId,
            Name,
            ParentPath,
            IncludeParent,
            DefaultForAllPosts,
            Notes
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<string>(Fields.ParentPath, reader, table);
            GetReaderValue<bool>(Fields.IncludeParent, reader, table);
            GetReaderValue<bool>(Fields.DefaultForAllPosts, reader, table);
            GetReaderValue<string>(Fields.Notes, reader, table);
        }

        public Hashtable GetById(int id, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByNameAndSiteId(string name, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetByNameAndSiteId");
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 20);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllBySiteId(int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetCollection (cmd);
        }

        public void Update(ref int id, int siteId, string name, string parentPath, bool includeParent, bool defaultForAllPosts, string notes, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 20);
            AddInputParameter(cmd, "ParentPath", parentPath, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "IncludeParent", includeParent, SqlDbType.Bit);
            AddInputParameter(cmd, "DefaultForAllPosts", defaultForAllPosts, SqlDbType.Bit);
            AddInputParameter(cmd, "Notes", notes, SqlDbType.VarChar, 1000);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "VersionIdOut", SqlDbType.Timestamp);
            ExecuteCommand(cmd);
            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "VersionIdOut");
            
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }
    }
}
